package com.hqyj.javaGraduation.hotel.controller;

import com.hqyj.javaGraduation.hotel.dao.HotelMaterialsBillDao;
import com.hqyj.javaGraduation.hotel.dao.HotelMaterialsDao;
import com.hqyj.javaGraduation.hotel.pojo.HotelComService;
import com.hqyj.javaGraduation.hotel.pojo.HotelMaterials;
import com.hqyj.javaGraduation.hotel.pojo.HotelMaterialsBill;
import com.hqyj.javaGraduation.hotel.service.HotelMaterialsBillService;
import com.hqyj.javaGraduation.hotel.service.HotelMaterialsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/Materials")
public class HotelMaterialsController {
    @Autowired
    HotelMaterialsService hotelMaterialsService;
    @Autowired
    HotelMaterialsBillService hotelMaterialsBillService;
    @Autowired
    HotelMaterialsBillDao hotelMaterialsBillDao;
    @Autowired
    HotelMaterialsDao hotelMaterialsDao;

    /**
     * 新增原材料
     * 127.0.0.1:8080/Materials/addMaterials    ---   get
     */
    @GetMapping("/addMaterials")
    public HashMap<String, Object> addMaterials(HotelMaterials hotelMaterials) {
        return hotelMaterialsService.addMaterials(hotelMaterials);
    }

    /**
     * 修改原材料
     * 127.0.0.1:8080/Materials/updateMaterials    ---   get
     */
    @GetMapping("/updateMaterials")
    public HashMap<String, Object> updateMaterials(HotelMaterials hotelMaterials) {
        return hotelMaterialsService.updateMaterials(hotelMaterials);
    }

    /**
     * 删除原材料
     * 127.0.0.1:8080/Materials/delMaterials    ---   get
     */
    @GetMapping("/delMaterials")
    public HashMap<String, Object> delMaterials(Integer id) {
        return hotelMaterialsService.delMaterials(id);
    }

    /**
     * 原材料列表
     * 127.0.0.1:8080/Materials/materialsList    ---   get
     */
    @GetMapping("/materialsList")
    public HashMap<String, Object> materialsList(HotelMaterials hotelMaterials) {
        return hotelMaterialsService.MaterialsList(hotelMaterials);
    }

    /**
     * 原材料列表
     * 127.0.0.1:8080/Materials/matSupList    ---   get
     */
    @GetMapping("/matSupList")
    public HashMap<String, Object> matSupList(HotelComService hotelComService, int matId) {
        return hotelMaterialsService.matSupList(hotelComService, matId);
    }

    /**
     * 进货
     * 127.0.0.1:8080/Materials/updateMatSup    ---   get
     */
    @GetMapping("/updateMatSup")
    public HashMap<String, Object> updateMatSup(HotelComService hotelComService) {
        return hotelMaterialsService.updateMatSup(hotelComService);
    }

    /**
     * 进货
     * 127.0.0.1:8080/Materials/matBillList    ---   get
     */
    @GetMapping("/matBillList")
    public HashMap<String, Object> matBillList(HotelMaterialsBill hotelMaterialsBill) {
        return hotelMaterialsBillService.matBillList(hotelMaterialsBill);
    }

    /**
     * 退货
     * 127.0.0.1:8080/Materials/delBill    ---   get
     */
    @GetMapping("/delBill")
    public HashMap<String, Object> delBill(int id) {
        return hotelMaterialsBillService.delBill(id);
    }

    /**
     * 退货
     * 127.0.0.1:8080/Materials/delBillMat    ---   get
     */
    @GetMapping("/delBillMat")
    public HashMap<String, Object> delBillMat(HotelMaterialsBill hotelMaterialsBill) {
        HashMap<String, Object> map = new HashMap<>();
        hotelMaterialsBill.setBillTime(new Date());

        HotelMaterials hotelMaterials = hotelMaterialsDao.selectById(hotelMaterialsBill.getMatId());
        hotelMaterials.setMatNumber(hotelMaterials.getMatNumber() - hotelMaterialsBill.getMatNumber());

        int num = hotelMaterialsDao.updateById(hotelMaterials);
        hotelMaterialsBill.setBillType("已退货");
        int num1=hotelMaterialsBillDao.updateById(hotelMaterialsBill);
        if (num > 0 && num1>0) {
            map.put("info", "退货成功");
        } else {
            map.put("info", "退货失败");
        }
        return map;

    }
}
